package cn.cdiothree.service.impl;

import cn.cdiothree.dao.ActivityInfoMapper;
import cn.cdiothree.dataobject.ActivityInfo;
import cn.cdiothree.dataobject.MyPageHelper;
import cn.cdiothree.service.ActivityInfoService;
import cn.cdiothree.vo.ActivityInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动信息服务实现类
 * @author cumthing
 */
@Service
@Slf4j
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {
    @Autowired
    private ActivityInfoMapper activityInfoMapper;

    /**
     * 根据类目获取活动信息
     * @param num
     * @param size
     * @param categoryId
     * @return
     */
    @Override
    public MyPageHelper<ActivityInfoVo> getActivityInfoVoListByCategoryId (int num, int size, Integer categoryId){
        QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
        activityInfoQueryWrapper.eq("category_id",categoryId);
        log.info("根据类目id{}查找活动",categoryId);
        PageHelper.startPage(num,size);
        List<ActivityInfo> activityInfoList = activityInfoMapper.selectList(activityInfoQueryWrapper);
        PageInfo<ActivityInfo> activityInfoPageInfo = new PageInfo<>(activityInfoList);
        List<ActivityInfoVo> activityInfoVoList =activityInfoList.stream().map(activityInfo -> {
            return assembleActivityInfo(activityInfo);
        }).collect(Collectors.toList());
        MyPageHelper<ActivityInfoVo> myPageHelper = new MyPageHelper<>();
        BeanUtils.copyProperties(activityInfoPageInfo,myPageHelper);
        myPageHelper.setList(activityInfoVoList);
        return myPageHelper;
    }

    /**
     * 获取所有活动信息
     * @param num
     * @param size
     * @return
     */
    @Override
    public MyPageHelper<ActivityInfoVo> getAll(int num, int size){
        log.info("查找所有活动");
        QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
        PageHelper.startPage(num,size);
        List<ActivityInfo> activityInfoList = activityInfoMapper.selectList(activityInfoQueryWrapper);
        PageInfo<ActivityInfo> activityInfoPageInfo = new PageInfo<>(activityInfoList);
        List<ActivityInfoVo> activityInfoVoList =activityInfoList.stream().map(activityInfo -> {
            return assembleActivityInfo(activityInfo);
        }).collect(Collectors.toList());
        MyPageHelper<ActivityInfoVo> myPageHelper = new MyPageHelper<>();
        BeanUtils.copyProperties(activityInfoPageInfo,myPageHelper);
        myPageHelper.setList(activityInfoVoList);
        return myPageHelper;
    }
    public ActivityInfoVo assembleActivityInfo(ActivityInfo activityInfo){
        ActivityInfoVo activityInfoVO = new ActivityInfoVo();
        BeanUtils.copyProperties(activityInfo,activityInfoVO);
        return activityInfoVO;
    }

    /**
     * 根据活动id查找活动
     * @param activityId
     * @return
     */
    @Override
    public ActivityInfo findByActivityId(Integer activityId){
        log.info("根据活动id{}查找活动",activityId);
        QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
        activityInfoQueryWrapper.eq("activity_id",activityId);
        ActivityInfo activityInfo = activityInfoMapper.selectOne(activityInfoQueryWrapper);
        return activityInfo;
    }

    /**
     * 根据组织id查找活动
     * @param organizationId
     * @return
     */
    @Override
    public List<ActivityInfoVo> selectByOrganizationId(Integer organizationId){
        log.info("根据组织id{}查找活动",organizationId);
        QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
        activityInfoQueryWrapper.eq("organization_id",organizationId);
        List<ActivityInfo> activityInfoList = activityInfoMapper.selectList(activityInfoQueryWrapper);

        List<ActivityInfoVo> activityInfoVoList =activityInfoList.stream().map(activityInfo -> {
            return assembleActivityInfo(activityInfo);
        }).collect(Collectors.toList());
        return activityInfoVoList;
    }
}
